use processed/RPImicro,clear

forvalues x = 1/17	 {
gen b`x' = s_RPI_`x'
drop if b`x' ==.
}

gen logI = log(I)
keep if I > 0
qui forvalues x = 1/17	 {
keep if b`x' <1 
keep if b`x' > -0.00001 
}

keep b* datayear I logI weight caseno

egen group_id = group(datayear caseno)
duplicates drop group_id ,force

save processed/index_tmp,replace

** Laspylas: 
use  processed/index_tmp, clear
replace datayear = datayear + 1
drop if datayear == 2018
merge m:1 datayear using processed/dlogp.dta
drop _merge 

reshape long b dlogp, i(group_id) j(goods_id)
bysort group_id datayear: egen dlogp_Las = total(b*dlogp)

duplicates drop datayear caseno,force
drop group_id	goods_id b dlogp
drop if I ==.
replace datayear = datayear - 1

save processed/index_Las,replace

**Paas: 
use  processed/index_tmp, clear
replace datayear = datayear 
drop if datayear == 1974
merge m:1 datayear using processed/dlogp.dta
drop _merge 

reshape long b dlogp, i(group_id) j(goods_id)
bysort group_id datayear: egen dlogp_Paas = total(b*dlogp)

duplicates drop datayear caseno,force
drop group_id	goods_id b dlogp

save processed/index_Paas,replace


*********************
use processed/index_Paas,replace

merge m:1 caseno	datayear	weight	I	logI using processed/index_Las.dta
drop if I ==.
drop if I <0
sort datayear I 
gen dlogp_Las_hat = .
gen dlogp_Paas_hat = .


qui forvalues s = 1/500  {
preserve
bsample , strata(datayear)	

* Lowess
	lowess dlogp_Las logI if datayear ==1974 , generate(tmp) nograph
	replace dlogp_Las_hat = tmp if datayear == 1974
	drop tmp
	
forvalues t = 1975/2016 {
	
    foreach x in dlogp_Paas dlogp_Las {
        lowess `x' logI if datayear == `t', generate(tmp) nograph
        replace `x'_hat = tmp if datayear == `t'
        drop tmp
    }
}


	lowess dlogp_Paas logI if datayear ==2017 , generate(tmp) nograph
	replace dlogp_Paas_hat = tmp if datayear == 2017 
	drop tmp

*******
keep datayear I dlogp_Paas_hat dlogp_Las_hat weight caseno
*duplicates drop caseno datayear,force
drop caseno
replace dlogp_Paas_hat = 0 if dlogp_Paas_hat ==.
replace dlogp_Las_hat = 0 if dlogp_Las_hat ==.

outsheet using bootstrap_samples/csv/UK_Budget_boot_`s'.csv, comma replace nonames
restore
}

* Lowess
	lowess dlogp_Las logI if datayear ==1974 , generate(tmp) nograph
	replace dlogp_Las_hat = tmp if datayear == 1974
	drop tmp
	
forvalues t = 1975/2016 {
	
    foreach x in dlogp_Paas dlogp_Las {
        lowess `x' logI if datayear == `t', generate(tmp) nograph
        replace `x'_hat = tmp if datayear == `t' & flag_boot == 1
        drop tmp
    }
}


	lowess dlogp_Paas logI if datayear ==2017 , generate(tmp) nograph
	replace dlogp_Paas_hat = tmp if datayear == 2017 
	drop tmp

*******
keep datayear I dlogp_Paas_hat dlogp_Las_hat weight caseno
*duplicates drop caseno datayear,force
drop caseno
replace dlogp_Paas_hat = 0 if dlogp_Paas_hat ==.
replace dlogp_Las_hat = 0 if dlogp_Las_hat ==.

outsheet using bootstrap_samples/csv/UK_Budget_boot_0.csv, comma replace nonames
